package me.ingxin.android.tools;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by ingxin on 2022/9/7
 */
public class Hex {

    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    /**
     * 将byte数组转化成十六进制字符串
     *
     * @param bytes bytes
     */
    @Nullable
    public static String encode(@NonNull byte[] bytes) {
        if (bytes.length == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        // 27对应的十六进制为1b,对应的二进制是00011011
        // 取高位和低位：00011011-》0001,1011-》1,b
        for (byte aByte : bytes) {
            // 取高位：跟0xf0做与运算后再右移4位
            int high = (aByte & 0xf0) >> 4;
            // 取低位：跟0x0f做与运算
            int low = aByte & 0x0f;
            // 字符映射
            sb.append(HEX_CHAR[high]).append(HEX_CHAR[low]);
        }
        return sb.toString();
    }

    /**
     * 将16进制字符串解析成byte数组
     *
     * @param hex 16进制字符串
     * @return 解析结果
     */
    @Nullable
    public static byte[] decode(@NonNull String hex) {
        if (hex.length() == 0) {
            return null;
        }
        // 16进制转byte，长度减半，"1b"-->27
        int len = hex.length() / 2;
        byte[] result = new byte[len];
        String highStr;
        String lowStr;
        int high;
        int low;
        for (int i = 0; i < len; i++) {
            // 高位值
            highStr = hex.substring(i * 2, i * 2 + 1);// "1b"的高位为"1"
            high = Integer.parseInt(highStr, 16);// 高位转为10进制
            // 低位值
            lowStr = hex.substring(i * 2 + 1, i * 2 + 2);// "1b"的低位为"b"
            low = Integer.parseInt(lowStr, 16);// 低位转为10进制
            // 合计值
            result[i] = (byte) ((high << 4) + low);// 相当于:(高位*16) + 低位
        }
        return result;
    }

}
